package cn.justforchris.system.controller;


import cn.justforchris.system.domain.Logininfo;
import cn.justforchris.system.query.LogininfoQuery;
import cn.justforchris.system.service.ILogininfoService;
import cn.justforchris.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/logininfo")
@Api(tags = "用户接口")
public class LogininfoController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 新增
     * @param logininfo
     * @return
     */
    @PutMapping
    public JsonResult insertOrUpdate(@RequestBody Logininfo logininfo) {
       try {
        if (logininfo.getId() == null) {
            logininfoService.insertLogininfo(logininfo);
        } else {
            logininfoService.updateLogininfo(logininfo);
        }
        return JsonResult.success();
       }catch (Exception e) {
           e.printStackTrace();
           return JsonResult.error();
       }
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deletLogininfo(@PathVariable Long id){
        try{
            logininfoService.deleteLogininfo(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDelet(@RequestBody Long[] ids){
        try{
            logininfoService.patchDelete(ids);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Logininfo getLogininfo(@PathVariable Long id){

        return logininfoService.getLogininfoById(id);

    }

    /**
     * 获取所有
     * @return
     */
    @GetMapping
    public List<Logininfo> getAll(){
        return logininfoService.getAll();
    }

    /**
     * 分页查询
     * @param queryPage
     * @return
     */
    @PostMapping
    public PageInfo<Logininfo> queryPage(@RequestBody LogininfoQuery queryPage){
        /*返回的数据是pagehelper的一个类,这个类创建的对象中储存的是查询出来的数据以及总条数(total)*/
        return logininfoService.queryPage(queryPage);

    }







}
